/**
 * This file holds the ViewSystemTitle class.
 */
package edu.gatech.spacetraderteam3.view;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import edu.gatech.spacetraderteam3.controller.GUIEventHandler;
import edu.gatech.spacetraderteam3.model.GameState;

/**
 * A seamless tile for the background of a screen
 * @author Alexander Ikonomidis
 * @version 1.0
 */
public class ViewSystemTitle extends ViewSystem {
    /**
     * The title image.
     */
    private static final BufferedImage TITLE_IMAGE = Assets.MiscImage.TITLE.image();

    /**
     * The window to hold the title image.
     */
    private final SGGWindow titleWindow;

    /**
     * The window to show if no save file was found.
     */
    private final SGGWindow noSaveWindow;

    /**
     * Creates a new title view
     */
    public ViewSystemTitle() {
        titleWindow = getTitleWindow();
        noSaveWindow = new SGGWindow(0, 0, 300, 120, GameState.TITLE);
        noSaveWindow.setAlignment(SGGWindow.Alignment.CENTER);
        final SGGButton button = new SGGButton("OKAY");
        button.setSGGEventListener(new SGGEventListener() {
            public void doEvent(SGGEvent sgge) {
                noSaveWindow.setVisible(false);
                titleWindow.setFocusable(true);
            }
        });
        noSaveWindow.add(button, 170, 40);
        noSaveWindow.setVisible(false);
        noSaveWindow.add(new SGGLabel("No save found!"), 40, 50);
        setStarVelocity(-1, 0);
        drawPlanets(true);
    }

    /**
     * @return a newly built title window
     */
    private SGGWindow getTitleWindow() {
        final SGGWindow window = new SGGWindow(0, 0, TITLE_IMAGE.getWidth(),
                TITLE_IMAGE.getHeight(), GameState.TITLE);
        window.setAlignment(SGGWindow.Alignment.CENTER);
        window.showContainer(false);
        final SGGLabel picture = new SGGLabel(TITLE_IMAGE);
        picture.setFocusable(false);
        window.add(picture, 0, 0);
        final SGGButton begin = new SGGButton(Assets.ButtonType.TITLE_NEW);
        begin.setSGGEventListener(new SGGEventListener() {
            public void doEvent(SGGEvent sgge) {
                GUIEventHandler.newGame();
            }
        });
        final SGGButton resume = new SGGButton(Assets.ButtonType.TITLE_CONTINUE);
        resume.setSGGEventListener(new SGGEventListener() {
            public void doEvent(SGGEvent sgge) {
                GUIEventHandler.continueGame();
            }
        });
        window.add(begin, 200, 225);
        window.add(resume, 170, 260);
        return window;
    }

    /**
     * Paints a the title view
     * @param g graphics object
     */
    public void paint(Graphics2D g) {
        super.paint(g);
        titleWindow.paint(g);
        noSaveWindow.paint(g);
    }
    
    /**
     * displayed after the user tries to continue without a save file
     */
    public void showNoSaveMessage() {
        titleWindow.setFocusable(false);
        noSaveWindow.setVisible(true);
    }

    /**
     * @return this is the title screen
     */
    public String toString() {
        return "This is the title Screen.";
    }
}
